<?php
require_once 'script.php';
include("Hierarchy.php");
include("File.php");

if($_GET['actiune'] == "adaugare"){
    $parent = Hierarchy::findById($_GET['hierarchy']); // get the parent
    $hierarchy = new Hierarchy();
    $hierarchy->IdHash = $parent->IdHash;
    $hierarchy->ParentId = $parent->Id;
    $hierarchy->CreateDate = date('y-m-d H:i:s');
    $hierarchy->LastUpdateDate = date('y-m-d H:i:s');
    $hierarchy->Ip = $_SERVER['REMOTE_ADDR'];
    $hierarchy->save(); // save new hierarchy

    $file = new File(); 
    $file->Name = $_GET['Name'];
    $file->HierarchyId = $hierarchy->Id;
    $file->Size = $_GET['Size'];
    $file->FileTypeId = $_GET['Filetype'];
    $file->CreateDate = date('y-m-d H:i:s');
    $file->LastUpdateDate = date('y-m-d H:i:s');
    $file->Ip = $_SERVER['REMOTE_ADDR'];
    $file->save();  //save new file info
    
    header('Location: index.php?hierarchy='.$_GET['hierarchy']);
}

if($_GET['fileId'] == null){
?>
    <form name='form1' enctype='multipart/form-data' method='get' action='addFile.php'>
        Nume: 
        <input id='Name' type='text' name='Name'>
        <br/>
        Marime:
        <input id='Size' type='text' name='Size'>
        <br/>
        Tip:
        <select name="Filetype">
            <?php
                $sql = "SELECT * FROM filetype";
                $result = mysql_query($sql);
                while($res = mysql_fetch_array($result)){
                    echo "<option value = " . $res['Id'] . ">" . $res['Name'] . "</option>";
                }
            ?>
        </select>
        <input name='actiune' type='hidden' value='adaugare'>
        <?php
        echo "<input name='hierarchy' type='hidden' value='" . $_GET['hierarchy'] . "'>";
        ?>
        <br/>
        <input type='submit' value='Ad&#259;ugare' name='Submit' role='button' aria-disabled='false'>
        <?php
        echo "<INPUT TYPE=BUTTON VALUE='&Icirc;napoi' CLASS=b ONCLICK='window.location=\"index.php?hierarchy=" . $_GET['hierarchy'] . "\";'>";
        ?>
    </form>
<?php
}
?>
